﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LPNGenerate
    {
        private static readonly IInventoryMgtDA InventoryMgtDA = DataAccess.CreateInventoryMgtDA();
        private static readonly IInvLotLocLpnDA invLotLocLpnDA = DataAccess.CreateInvLotLocLpnDA();
        private static readonly ILpnDA lpnDA = DataAccess.CreateLpnDA();
        private static readonly IReceiptDA receiptDA = DataAccess.CreateReceiptDA();
        private static readonly IReceiptDetailDA receiptDetailDA = DataAccess.CreateReceiptDetailDA();

        public string SkyTransLpnGenerate(LPNGenParams lpnGenParams, PubParasInfo pubParasInfo, string whLoginID)
        {
            int i;
            string temp = string.Empty;
            if (lpnGenParams.Count >= 0x2710)
            {
                return "请输入小于10000的生成数量！";
            }
            string mon = (DateTime.Now.Month.ToString().Length == 1) ? ("0" + DateTime.Now.Month.ToString()) : DateTime.Now.Month.ToString();
            string firstNum = DateTime.Now.Year.ToString().Substring(2, 2) + mon;
            string secondNum = string.Empty;
            string thirdNum = string.Empty;
            string oldLpnID = lpnDA.GetMaxLpnByDocID(lpnGenParams.DocID, lpnGenParams.DocType, whLoginID);
            if (!string.IsNullOrEmpty(oldLpnID))
            {
                if (oldLpnID.Length == 0x11)
                {
                    firstNum = oldLpnID.Substring(0, 4);
                    secondNum = oldLpnID.Substring(4, 5);
                    thirdNum = lpnDA.GetNextLpnByDocID(lpnGenParams.DocID, lpnGenParams.DocType, whLoginID);
                }
                else if (oldLpnID.Length == 0x10)
                {
                    firstNum = oldLpnID.Substring(0, 4);
                    secondNum = oldLpnID.Substring(4, 5);
                    thirdNum = lpnDA.GetNextLpnByDocID(lpnGenParams.DocID, lpnGenParams.DocType, whLoginID);
                }
            }
            else
            {
                ReceiptQueryEntity receiptQueryEntity = new ReceiptQueryEntity {
                    IsGetAll = true,
                    SortField = "R.CREATED_DATE",
                    SortDirection = "ASC"
                };
                List<string> sqlWhere = new List<string> {
                    " R.CREATED_DATE >='" + DateTime.Now.ToString("yyyy-MM-01 00:00:00") + "'",
                    " R.CREATED_DATE <='" + DateTime.Now.AddMonths(1).AddDays((double) (DateTime.Now.Day * -1)).ToString("yyyy-MM-dd 23:59:59") + "'"
                };
                DataSet ds = receiptDA.GetReceiptByQueryList(sqlWhere, receiptQueryEntity, whLoginID, pubParasInfo);
                if (((ds != null) && (ds.Tables.Count > 0)) && (ds.Tables[0].Rows.Count > 0))
                {
                    for (i = 0; ds.Tables[0].Rows.Count > i; i++)
                    {
                        if (ds.Tables[0].Rows[i]["RECEIPT_ID"].ToString() == lpnGenParams.DocID)
                        {
                            int CS00003 = i + 1;
                            secondNum = CS00003.ToString();
                            break;
                        }
                    }
                }
                else
                {
                    secondNum = "1";
                }
                secondNum = this.ValueConvert(secondNum, 5);
                if (lpnDA.CheckLpnIDUnique(firstNum + secondNum + "00010000", whLoginID))
                {
                    i = 1;
                    while (true)
                    {
                        string strsecondnum = this.ValueConvert((int.Parse(secondNum) + i).ToString(), 5);
                        if (!lpnDA.CheckLpnIDUnique(firstNum + strsecondnum + "00010000", whLoginID))
                        {
                            secondNum = strsecondnum;
                            break;
                        }
                        i++;
                    }
                }
                thirdNum = "0";
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                try
                {
                    int ithirdNum = int.Parse(thirdNum);
                    for (i = 0; lpnGenParams.Count > i; i++)
                    {
                        if (ithirdNum < 0x270f)
                        {
                            LpnInfo lpn;
                            ithirdNum++;
                            thirdNum = this.ValueConvert(ithirdNum.ToString(), 4);
                            lpn = new LpnInfo();
                            lpn.LpnID = firstNum + secondNum + thirdNum + "0000";
                            lpn.Qty = 0M;
                            lpn.Status = "OK";
                            lpn.PackID = "";
                            lpn.DocID = lpnGenParams.DocID;
                            lpn.DocType = LpnDocType.InBound;
                            lpn.CreatedBy = pubParasInfo.UserID;
                            lpn.UpdatedBy = lpn.CreatedBy;
                            lpn.IsActive = "Y";
                            lpnDA.InsertLpn(dataBase, tran, lpn, whLoginID);
                            temp = temp + lpn.LpnID + "<br/>";
                        }
                        else
                        {
                            break;
                        }
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                }
                return temp;
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string SkyTransLpnSplit(List<LpnInfo> NewLpnInfo, InventoryInfo inventoryInfo, string strLpnID, PubParasInfo pubParasInfo, string whLoginID)
        {
            string temp = string.Empty;
            LpnInfo OldLpnInfo = lpnDA.GetLpnByID(strLpnID, whLoginID);
            if (inventoryInfo != null)
            {
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    try
                    {
                        foreach (LpnInfo info in NewLpnInfo)
                        {
                            InvLotLocLpnInfo invLotLocLpn;
                            invLotLocLpn = new InvLotLocLpnInfo();
                            invLotLocLpn.AllocatedQty = 0M;
                            invLotLocLpn.ArchiveDate = DateTime.Now;
                            invLotLocLpn.ArchiveQty = 0M;
                            invLotLocLpn.AvailableQty = 0M;
                            invLotLocLpn.CreatedBy = pubParasInfo.UserID;
                            invLotLocLpn.ExpectedQty = 0M;
                            invLotLocLpn.Loc = inventoryInfo.Loc;
                            invLotLocLpn.Lot = inventoryInfo.Lot;
                            invLotLocLpn.LpnID = info.LpnID;
                            invLotLocLpn.OwnerID = inventoryInfo.OwnerID;
                            invLotLocLpn.PendingMoveIn = 0M;
                            invLotLocLpn.PickedQty = 0M;
                            invLotLocLpn.PickinprocessQty = 0M;
                            invLotLocLpn.Qty = info.Qty;
                            invLotLocLpn.SkuID = inventoryInfo.SkuID;
                            invLotLocLpn.Status = inventoryInfo.Status;
                            invLotLocLpn.UpdatedBy = invLotLocLpn.CreatedBy;
                            info.PackID = OldLpnInfo.PackID;
                            info.IsActive = OldLpnInfo.IsActive;
                            info.DocID = OldLpnInfo.DocID;
                            info.DocType = OldLpnInfo.DocType;
                            info.Ipskey = OldLpnInfo.Ipskey;
                            info.PutawayHi = OldLpnInfo.PutawayHi;
                            info.PutawayTi = OldLpnInfo.PutawayTi;
                            lpnDA.InsertLpn(dataBase, tran, info, whLoginID);
                            invLotLocLpnDA.InsertInvLotLocLpn(dataBase, tran, invLotLocLpn, whLoginID);
                        }
                        OldLpnInfo.IsActive = "N";
                        OldLpnInfo.Qty = 0M;
                        lpnDA.UpdateLpn(dataBase, tran, OldLpnInfo, whLoginID);
                        invLotLocLpnDA.UpdateInvLotLocLpnQty(dataBase, tran, 0M, inventoryInfo.Loc, inventoryInfo.Lot, inventoryInfo.LpnID, pubParasInfo.UserID, whLoginID);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                    return temp;
                }
                finally
                {
                    conn.Close();
                }
                return temp;
            }
            return "未找到匹配的数据进行拆分！";
        }

        public string ValueConvert(string value, int num)
        {
            string temp = value;
            if (value.Length < num)
            {
                for (int i = value.Length; num > i; i++)
                {
                    temp = "0" + temp;
                }
            }
            return temp;
        }
    }
}

